package {
	import flash.desktop.NativeProcess;
	import flash.desktop.NativeProcessStartupInfo;
	import flash.filesystem.File;
	
	import flash.events.ProgressEvent;
	import mx.controls.Alert;
			
	public class PRun {
		
		
		public var process:NativeProcess;
		private var fLog:Function;
		private var socStateChange:Function;
		
		public function run(name){
			
				if (!NativeProcess.isSupported){
	                    fLog('PRun: error nativeProcess is not supported');
	                    return 0;
                }
                
               
                
                var arg = name.match(/(?<=\s)[^\s]+/g);
                name = name.match(/[^\s]+/);
                
                //Alert.show(arg);
                
                try { 
	                var file:File = new File(name);
	                
	                process = new NativeProcess();
	                var nativeProcessStartupInfo:NativeProcessStartupInfo = new NativeProcessStartupInfo();
	                nativeProcessStartupInfo.executable = file;
	                    
	                var args:Vector.<String> = new Vector.<String>;
	                for (var i in arg)
	                	args.push(arg[i]);
	                nativeProcessStartupInfo.arguments = args;
	                    
	                process.start(nativeProcessStartupInfo);
	                process.addEventListener(ProgressEvent.STANDARD_OUTPUT_DATA, stdoutHandler);
	                process.addEventListener(ProgressEvent.STANDARD_ERROR_DATA, errorHandler);
                    process.addEventListener("standardOutputClose", function(){if (socStateChange) socStateChange('ON_EXIT_WCOM'); /*fLog('close most ...');*/ });
                }
	            catch (e:Error)
	                {
	                    //Alert.show(e.message, "Error");
	                    if (fLog) fLog(e.message);
	                    return 0;
	                }
            
            
	             function stdoutHandler(event:ProgressEvent):void {
	                var process:NativeProcess = event.target as NativeProcess;
	                //var data:String = process.standardOutput.readUTFBytes(process.standardOutput.bytesAvailable);
	                var data:String = process.standardOutput.readMultiByte(process.standardOutput.bytesAvailable, "cp866");
	                fLog('PRun: '  + data);
	            }
	            
	             function errorHandler(event:ProgressEvent):void {
	                var process:NativeProcess = event.target as NativeProcess;
	                var data:String = process.standardError.readUTFBytes(process.standardError.bytesAvailable);
	                fLog('PRun: error '  + data);
	            }
               
               return process;  	
		}
		
		
		public function exit(){
			 if (process && process.running)  process.exit(true);
		}
		
		
		public function PRun(name :* = null, fLog_:Function = null, socStateChange_ = null){
			
			
			if (name) run(name);	
			fLog = fLog_;
			socStateChange = socStateChange_;
				
		}



		
	}
}